<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台区域控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class AddressController extends AdminController {

    /**
     * 区域列表管理
     * @return none
     */
    public function index(){
        $list   = $this->lists(M('address'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '区域列表';
        $this->display();
    }

    public function addAddress(){
        $this->meta_title = '新增区域';
        $this->display('edit_address');
    }

    public function editAddress(){
        $id=I('get.id');
        $data=M('address')->where(array('id'=>$id))->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑区域';
        $this->display('edit_address');
    }

    public function saveAddress(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('address')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('index') );
            }
        } else { //更新数据
            $status = M('address')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('index') );
            }
        }
    }


    /**修改状态
     * @param array $where
     */
    public function changeStatus() {
        $ids    =   I('request.ids');
        $status    =   I('request.status');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
        }else{
            $where['id']=$ids;
        }

        if( M('address')->where($where)->setField('status',$status) !== false) {
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败！','',true);
        }

    }




    


}
